package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.User.bouns.BounsItem;
import cn.kooki.app.duobao.util.StringUtils;
import cn.kooki.app.duobao.util.TimeUtils;

/**
 * Created by xiong on 2015/12/16.
 */
public class BounsItemAdapter extends BaseAdapter {

    private final float smallsize;
    private Context context;
    private Resources r;
    private ArrayList<BounsItem> datas;
    private final float noramlsize;

    public BounsItemAdapter(Context context, ArrayList<BounsItem> datas) {
        this.context = context;
        this.datas = datas;
        r = context.getResources();
        noramlsize = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 14, r.getDisplayMetrics());
        smallsize = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 11, r.getDisplayMetrics());
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BounsItem bounsItem = datas.get(position);

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_duobao_item_bonus, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.bonusName.setText(bounsItem.getTitle());

        viewHolder.bonusPrice.setText(bounsItem.getMoney());
        String balance = "余 额 ：" + bounsItem.getMoney() + "抢宝币";
        SpannableStringBuilder builder = new SpannableStringBuilder(balance);
        builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.main_red)), 5, balance.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.bonusBalance.setText(builder);
        viewHolder.bonusExpire.setText("有效期: " + TimeUtils.getTime(bounsItem.getEndtime()));
        String limit = null;
        if (StringUtils.nullSafeParseInt(bounsItem.getFull_money()) > 0) {
            limit = "单笔支付满" + bounsItem.getFull_money() + "抢宝币时可使用(" + bounsItem.getContent() + ")";
        } else {
            limit = bounsItem.getContent();
        }
        viewHolder.bonusLimit.setText(limit);
        return convertView;
    }


    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'layout_duobao_item_bonus.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.bonus_boxbg)
        LinearLayout bonusBoxbg;
        @Bind(R.id.bonus_name)
        TextView bonusName;
        @Bind(R.id.bonus_balance)
        TextView bonusBalance;
        @Bind(R.id.bonus_expire)
        TextView bonusExpire;
        @Bind(R.id.bonus_limit)
        TextView bonusLimit;
        @Bind(R.id.iv_arr)
        ImageView ivArr;
        @Bind(R.id.bonus_showuse)
        LinearLayout bonusShowuse;
        @Bind(R.id.iv_icoStandby)
        ImageView ivIcoStandby;
        @Bind(R.id.iv_icoPast)
        ImageView ivIcoPast;
        @Bind(R.id.duobao_bonus_wrapper)
        RelativeLayout duobaoBonusWrapper;
        @Bind(R.id.bonus_price)
        TextView bonusPrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
